<template>
  <div
    class="carousel-item-container"
    ref="container"
    @mousemove="mousemoveHandle"
    @mouseleave="mouseleaveHandle"
  >
    <div class="carousel" ref="image" :style="imagePosition">
      <ImageLoader
        :src="data.bigImg"
        :placeholder="data.midImg"
        @load="this.showWords"
      />
    </div>
    <div class="title" ref="title">{{ data.title }}</div>
    <div class="desc" ref="desc">{{ data.description }}</div>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader/index.vue";
export default {
  components: {
    ImageLoader,
  },
  data() {
    return {
      titleWidth: 0,
      descWidth: 0,
      containerSize: null,//图片容器宽高对象
      imageSize: null,//图片宽高对象
      mouseX: 0,//鼠标相对图片的x坐标
      mouseY: 0,//鼠标相对图片的Y坐标
    };
  },
  props: ["data"],
  computed: {
    imagePosition() {
      if (!this.imageSize || !this.containerSize) {
        return {};
      }
      const extraWidth = this.imageSize.width - this.containerSize.width;
      const extraHeight = this.imageSize.height - this.containerSize.height;
      const left = -(extraWidth / this.containerSize.width) * this.mouseX;
      const top = -(extraHeight / this.containerSize.height) * this.mouseY ;
      return {
       transform:`translate(${left}px,${top}px)`,
      };
    },
    center(){
      return {
        left:this.containerSize.width/2,
        top: this.containerSize.height/2,
      }
    }
  },
  mounted() {
    this.titleWidth = this.$refs.title.clientWidth;
    this.descWidth = this.$refs.desc.clientWidth;
    this.setSize();
    this.mouseX = this.center.left;
    this.mouseY = this.center.top;
    window.addEventListener("resize", this.setSize);
  },
  destroyed() {
    window.removeEventListener("resize", this.setSize);
  },
  methods: {
    showWords() {
      this.$refs.title.style.opacity = 1;
      this.$refs.title.style.width = 0;
      this.$refs.title.style.transition = "1s";
      //强制渲染
      this.$refs.title.clientWidth;
      this.$refs.title.style.width = this.titleWidth + "px";

      this.$refs.desc.style.opacity = 1;
      this.$refs.desc.style.width = 0;
      this.$refs.desc.style.transition = "2s 1s";
      //强制渲染
      this.$refs.desc.clientWidth;
      this.$refs.desc.style.width = this.descWidth + "px";
    },
    setSize() {
      this.containerSize = {
        width: this.$refs.container.clientWidth,
        height: this.$refs.container.clientHeight,
      };
      this.imageSize = {
        width: (this.imageSize = this.$refs.image.clientWidth),
        height: (this.imageSize = this.$refs.image.clientHeight),
      };
    },
    mousemoveHandle(e) {
      const rect = this.$refs.container.getBoundingClientRect();
      this.mouseX = e.clientX - rect.left;
      this.mouseY = e.clientY - rect.top;
    },
    mouseleaveHandle(){
      this.mouseX = this.center.left;
      this.mouseY = this.center.top;
    }
  },
};
</script>

<style lang="less" scoped>
.carousel-item-container {
  background-color: white;
  color: #fff;
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  .carousel {
    width: 110%;
    height: 110%;
    position: relative;
    transition: .25s;
    left: 0;
    top: 0;
  }
  .title,
  .desc {
    opacity: 0;
    position: absolute;
    left: 30px;
    color: #fff;
    letter-spacing: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-shadow: 1px 0 0 rgba(0, 0, 0, 0.5), 0 -1px 0 rgba(0, 0, 0, 0.5),
      -1px 0 0 rgba(0, 0, 0, 0.5), 0 1px 0 rgba(0, 0, 0, 0.5);
  }
  .title {
    top: calc(~"50% - 40px");
    font-size: 2em;
  }
  .desc {
    top: calc(~"50% + 20px");
    font-size: 1.2em;
  }
}
</style>